#include <iostream>
#include <string>

using namespace std;


class Solution
{
public:
    string convert(string s, int nRows)
    {
        string result="";
        if(s=="")
            return result;
        if(nRows==1)
            return s;
        int groupsize=2*(nRows-1);
        int slength=s.length();
        int group=slength%groupsize==0?slength/groupsize:slength/groupsize+1;
        for(int j=nRows-1; j>=0; --j)
            {
                for(int i=0; i<group; ++i)
                    {
                        int k=i*groupsize+nRows;

                        if(slength>=k-j)
                            result+=s[k-j-1];
                        if(j<nRows-1&&j>0)
                            {
                                if(slength>=k+j)
                                    result+=s[k+j-1];
                            }
                    }
            }
        return result;
    }
};

int main()
{
    Solution solution;
    string s="ABCD";
    cout<<solution.convert(s,3)<<endl;
}